<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Business;
use app\common\model\Category;
use app\common\model\Goods;
use app\common\model\GoodsComment;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     * @param string $category_id 分类ID，默认为0
     * @param string $business_id 厂家ID，默认为0
     * @param string $category_name 分类名称，默认为null
     * @param string $business_name 厂家名称，默认为null
     * @param string $name 产品名称，默认为null
     * @param string $page 页数（默认为1）
     * @ApiReturn   ({
    "code": 200,
    "msg": "请求成功",
    "time": "1606187829",
    "data": [{
    "id": 2,
    "name": "雷蛇机械键盘",产品名称
    "intro": "这是一个机械键盘的简介。这是一个机械键盘的简介。",产品简介
    "images": "http:\/\/www.chanpin.com\/uploads\/20201123\/b612809cdaded818a367ec987d510786.png",产品缩略图
    "price": "5800.00",产品价格
    "comment": 0评价数量
    }, {
    "id": 1,
    "name": "华为mate平板",
    "intro": "这是一个平板的简介。这是一个平板的简介。",
    "images": "http:\/\/www.chanpin.com\/uploads\/20201123\/b612809cdaded818a367ec987d510786.png",
    "price": "5800.00",
    "comment": 5
    }]
    })
     */
    public function index()
    {
        $category_id  = $this->request->get('category_id')==null?0:$this->request->get('category_id');
        $business_id  = $this->request->get('business_id')==null?0:$this->request->get('business_id');
        $category_name  = $this->request->get('category_name')==null?'':$this->request->get('category_name');
        $business_name  = $this->request->get('business_name')==null?'':$this->request->get('business_name');
        $name  = $this->request->get('name')==null?'':$this->request->get('name');
        $page  = $this->request->get('page')==null?1:$this->request->get('page');
        $pagesize = 10;
        $page = ($page-1)*$pagesize;
        $where = array();
        if($category_id){
            $where['c.id'] = array('eq',$category_id);
        }
        if($business_id){
            $where['b.id'] = array('eq',$business_id);
        }
        if($category_name){
            $where['c.name'] = array('like','%'.$category_name.'%');
        }
        if($business_name){
            $where['b.name'] = array('like','%'.$business_name.'%');
        }
        if($name){
            $where['g.name'] = array('like','%'.$name.'%');
        }
        $list = Goods::alias('g')->join('category c','c.id = g.category_id')->join('business b','b.id = g.business_id')->where($where)->field('g.id,g.name,g.intro,g.images,g.price')->limit($page,$pagesize)->order('g.id desc')->select();
        foreach($list as &$value){
            $images = explode(',',$value['images']);
            $value['images'] = $this->domain.$images[0];
            $value['comment'] = GoodsComment::where('goods_id',$value['id'])->group('userid')->count();
        }
        $this->success($list);
    }
    /**
     * 分类列表
     * @ApiReturn   ({
    "code": 200,
    "msg": "请求成功",
    "time": "1606187994",
    "data": [{
    "id": 1,分类ID
    "name": "手机"分类名称
    }, {
    "id": 2,
    "name": "电脑"
    }, {
    "id": 3,
    "name": "平板"
    }, {
    "id": 4,
    "name": "配件"
    }, {
    "id": 5,
    "name": "鼠标"
    }, {
    "id": 6,
    "name": "键盘"
    }]
    })
     */
    public function category()
    {
        $list = Category::order('id asc')->field('id,name')->select();
        $this->success($list);
    }
    /**
     * 分类列表
     * @param int $category_id 分类ID
     * @ApiReturn   ({
    "code": 200,
    "msg": "请求成功",
    "time": "1606188639",
    "data": [{
    "id": 2,厂家ID
    "name": "雷蛇",厂家名称
    "image": "http:\/\/www.chanpin.com\/uploads\/20201123\/105f6bbfbaccd0f19b7b4075d95d7938.png"厂家图片
    }, {
    "id": 3,
    "name": "小米",
    "image": "http:\/\/www.chanpin.com\/uploads\/20201123\/1002c23f784becda30280a503e9dfb59.png"
    }]
    })
     */
    public function business_list()
    {
        $category_id  = $this->request->get('category_id');
        //$sql = "select id,name,image from fa_business WHERE find_in_set($category_id,category_ids) ORDER id asc";
        //$list = Db::query($sql);
        $list = Business::where('find_in_set(:category_id,category_ids)',['category_id'=>$category_id])->order('id asc')->field('id,name,image')->select();
        foreach($list as &$value){
            $value['image'] = $this->domain.$value['image'];
        }
        $this->success($list);
    }
}
